package com.prj.ufdm.webflux.lambda;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

public class PredicateTest {

	public static void main(String[] args) {
		
		Predicate<String> predicate = item -> "hello".equals(item);
	    // test 方法测试
	    System.out.println("1---> " + predicate.test("你好"));

        // Predicate 返回一个List中的偶数
	    List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
	    // list.stream(), 表示将List作为流进行处理, filter()方法接收一个Predicate, toArray是将流转换成数组
        Object[] result = list.stream().filter(t -> t % 2 == 0).toArray();
        System.out.println("2---> " + Arrays.toString(result));
        
		// 字符串过滤
		 List<String> languages = Arrays.asList("Java", "Scala", "C++", "Haskell", "Lisp");
		 System.out.println("Languages which starts with J :");
		 filter(languages, s -> s.startsWith("J"));

	}
	
	public static void filter(List<String> names, Predicate<String> condition) {
		
//		names.stream()
//			.filter( t -> condition.test(t) )
//			.forEach( System.out::println );
		
//		names.stream().filter((name) -> (condition.test(name))).forEach((name) -> {
//	        System.out.println(name + " ");
//	    });
		
	    for(String name: names)  {
	        if(condition.test(name)) {
	            System.out.println(name + " ");
	        }
	    }
	}

}
